package org.dragonnova.meetingapp.model.bean;

import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.google.gson.GsonBuilder;

import org.dragonnova.meetingapp.utils.LogUtil;

import java.io.Serializable;

/**
 * 设备信息
 * Created by hucn on 2016/5/12.
 * DeviceInfo{hostId='5baa6b96a2e46276', btMacAddr='CC:A2:23:35:F1:68', wifiAddr='null', btName='WIFI语音服务', btStatus=DisConnected, wifiStatus=DisConnected, language=��, timestamp=0, port=0}
 */
public class DeviceInfo implements Serializable, Comparable, Cloneable {

    private String hostId;

    private String hostName;

    private String wifiMacAddr;

    private String btMacAddr;

    private String wifiAddr;
    //蓝牙名称
    private String btName;

    private Status btStatus = Status.DisConnected;

    private Status wifiStatus = Status.DisConnected;

    private char language;

    private long timestamp;

    private int port;

    private int disconnectedCount;

    private static final DeviceInfo LocalDeviceInfo = new DeviceInfo();

    public DeviceInfo() {

    }

    public DeviceInfo(String hostId, String hostName, String wifiMacAddr, String btMacAddr, String wifiAddr, String btName, char language, int port) {
        this.hostId = hostId;
        this.hostName = hostName;
        this.wifiMacAddr = wifiMacAddr;
        this.btMacAddr = btMacAddr;
        this.wifiAddr = wifiAddr;
        this.btName = btName;
        this.language = language;
        this.port = port;
    }


    public int getDisconnectedCount() {
        return disconnectedCount;
    }

    public void setDisconnectedCount(int disconnectedCount) {
        this.disconnectedCount = disconnectedCount;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public char getLanguage() {
        return language;
    }

    public void setLanguage(char language) {
        this.language = language;
    }

    public long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(long timestamp) {
        this.timestamp = timestamp;
    }

    public Status getWifiStatus() {
        return wifiStatus;
    }

    public void setWifiStatus(Status wifiStatus) {
        this.wifiStatus = wifiStatus;
    }

    public String getHostId() {
        return hostId;
    }

    public void setHostId(String hostId) {
        this.hostId = hostId;
    }

    public String getHostName() {
        return hostName;
    }

    public void setHostName(String hostName) {
        this.hostName = hostName;
    }

    public String getWifiMacAddr() {
        return wifiMacAddr;
    }

    public void setWifiMacAddr(String wifiMacAddr) {
        this.wifiMacAddr = wifiMacAddr;
    }

    public String getBtMacAddr() {
        return btMacAddr;
    }

    public void setBtMacAddr(String btMacAddr) {
        this.btMacAddr = btMacAddr;
    }

    public String getWifiAddr() {
        return wifiAddr;
    }

    public void setWifiAddr(String wifiAddr) {
        this.wifiAddr = wifiAddr;
    }

    public String getBtName() {
        return btName;
    }

    public void setBtName(String btName) {
        this.btName = btName;
    }

    public Status getBtStatus() {
        return btStatus;
    }

    public void setBtStatus(Status btStatus) {
        this.btStatus = btStatus;
    }

    public byte[] encode() {
        String msg = new GsonBuilder().create().toJson(this);
        LogUtil.eee("json============" + msg);
        return msg.getBytes();
    }

    public static DeviceInfo parse(String json) {
        return new GsonBuilder().create().fromJson(json, DeviceInfo.class);
    }

    public enum Status {
        Connected, DisConnected
    }

    @Override
    public int compareTo(@NonNull Object info) {
        if (info instanceof DeviceInfo) {
            return this.getHostId().compareTo(((DeviceInfo) info).getHostId());
        } else {
            throw new ClassCastException("Cannot compare Pair with " + info.getClass().getName());
        }
    }

    /**
     * MAC地址和名称决定唯一性
     */
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof DeviceInfo)) return false;
        DeviceInfo ipInfo = (DeviceInfo) o;
        return TextUtils.equals(getHostId(), ipInfo.getHostId());
    }

    @Override
    public int hashCode() {
        return getHostId().hashCode();
    }

    @Override
    public String toString() {
        return "DeviceInfo{" +
                "hostId='" + hostId + '\'' +
                ", btMacAddr='" + btMacAddr + '\'' +
                ", wifiAddr='" + wifiAddr + '\'' +
                ", btName='" + btName + '\'' +
                ", btStatus=" + btStatus +
                ", wifiStatus=" + wifiStatus +
                ", language=" + language +
                ", timestamp=" + timestamp +
                ", port=" + port +
                ", disconnectedCount=" + disconnectedCount +
                '}';
    }

    public Object clone() {
        DeviceInfo o = null;
        try {
            o = (DeviceInfo) super.clone();
        } catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
        return o;
    }
}
